define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'microend/healthy/index',
                    add_url: 'microend/healthy/add',
                    edit_url: 'microend/healthy/edit',
                    del_url: 'microend/healthy/del',
                    multi_url: 'microend/healthy/multi',
                    table: 'user_healthy',
                    ajax_url:{
                        btn_1:{data:{'type':"-1"},url:"microend/healthy/update_status",fc:function(data){
                                layer.msg("评分成功");
                                return true;
                            }},
                        btn_2:{data:{'type':"1"},url:"microend/healthy/update_status",fc:function(data){
                                return true;
                            }},
                        btn_3:{data:{'type':"2"},url:"microend/healthy/update_status",fc:function(data){
                                return true;
                            }},
                        btn_4:{data:{'type':"3"},url:"microend/healthy/update_status",fc:function(data){
                                return true;
                            }},
                    },
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'healthy_id',
                sortName: 'healthy_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'r.username_alias', title: __('Username'),operate: 'like'},
                        {field: 'r.tao_bao_no', title: __('Tao_bao_no')},
                        {field: 'r.mobile', title: __('Mobile')},
                        {field: 'healthy', title: __('Healthy'), searchList: {"0":__('0'),"1":__('1'),"2":__('2'),"3":__('3')}, formatter: Table.api.formatter.normal},
                        {field: 'tao_bao_info_img', title: __('Tao_bao_info_img'),formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index){

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                buttons.push(
                                    {
                                        name: 'btn_1',
                                        text: "0",
                                        extend: 'data-toggle="tooltip" data-name="btn_1"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                    },
                                    {
                                        name: 'btn_2',
                                        text: 1,
                                        extend: 'data-toggle="tooltip" data-name="btn_2"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                    },
                                    {
                                        name: 'btn_3',
                                        text: 2,
                                        extend: 'data-toggle="tooltip" data-name="btn_3"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                    },
                                    {
                                        name: 'btn_4',
                                        text: 3,
                                        extend: 'data-toggle="tooltip" data-name="btn_4"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    }
                                    );
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');

                            }
                        }
                    ]
                ]
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    if(row.tao_bao_info_img) {
                        var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                        return '<a href="' + row.tao_bao_info_img + '" target="_blank"><img src="' + row.tao_bao_info_img + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            }
        }
    };
    return Controller;
});